#coding:utf-8
import os,sys
from bs4 import BeautifulSoup 

class getdata(object):
	"""docstring for Dict"""
	def __init__(self, DICTIONARY = {}):
		self.DICTIONARY = {
			'Version':self.version(0),
			'All':self.all_data(0),
			'Pass':self.pass_data(0),
			'stablefail':8298,
			'stabledata':self.get_stablefail(1),
			'floatfail':self.get_floatfail(0),
			'floatdata':self.get_floatfail(1),
			'Add':self.get_add(0),
			'Add_data':self.get_add(1),
			'Crash':self.get_crash(0),
			'Crashdata':self.get_crash(1),

			'Image_Filure':self.get_Image_Filure(0),
			'Image_Filuredata':self.get_Image_Filure(1),
			'Image_Test_Fiure':self.get_Image_Test_Fiure(0),
			'Image_Test_Fiuredata':self.get_Image_Test_Fiure(1),
			'Missing':self.get_Missing(0),
			'Missingdata':self.get_Missing(1),
			'Skipeed':self.get_Skipeed(0),
			'Skipeeddata':self.get_Skipeed(1),
			'Text_Failure':self.get_Text_Failure(0),
			'Text_Failuredata':self.get_Text_Failure(1),
			'Timeout':self.get_Timeout(0),
			'Timeoutdata':self.get_Timeout(1)
		};

	def version(self,key=0):
		return 'chrome 63.0.3239.111'

	def all_data(self,key=0):
		return 48125

	def pass_data(self,key=0):
		return 47983

	def get_stablefail(self,key=0):
		with open('stablefail.txt') as fn:
			items = [x.split() for x in fn.readlines()][0]
		if key==1:
			return items
		return len(items)

	def get_floatfail(self,key=0):
		with open('floatfail.txt') as fn:
			items = [x.split() for x in fn.readlines()][0]
		if key==1:
			return items
		return len(items)

	def get_add(self,key=0):
		return 0

	def get_crash(self,key=0):
		try:
			if not 'Crash.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Crash')
			while True:
				with open('Crash.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Image_Filure(self,key=0):
		try:
			if not 'Image_Filure.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Image_Filure')
			while True:
				with open('Image_Filure.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Image_Test_Fiure(self,key=0):
		try:
			if not 'Image_Test_Fiure.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Image_Test_Fiure')
			while True:
				with open('Image_Test_Fiure.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Missing(self,key=0):
		try:
			if not 'Missing.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Missing')
			while True:
				with open('Missing.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Skipeed(self,key=0):
		try:
			if not 'Skipeed.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Skipeed')
			while True:
				with open('Skipeed.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Text_Failure(self,key=0):
		try:
			if not 'Text_Failure.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Text_Failure')
			while True:
				with open('Text_Failure.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e

	def get_Timeout(self,key=0):
		try:
			if not 'Timeout.txt' in os.listdir(os.getcwd()):
				self.html_process_txt('Timeout')
			while True:
				with open('Timeout.txt') as fn:
					items = [x.split() for x in fn.readlines()][0]
				if key==1:
					return items
				return len(items)
		except Exception, e:
			raise e
		
	def html_process_txt(self,path=''):
		fns = [os.path.join(root, fn) for root, dirs, files in os.walk(os.path.join(os.getcwd(),path)) for fn in files if '.html' in fn]
		l1 = []
		try:
			for fn in fns:
				soup = BeautifulSoup(open(fn),'html.parser')
				for title in soup.select('a'):
					l1.append(title.get('href'))
			l2 = sorted(set(l1),key=l1.index)
			hf = open(path+'.txt','wb')
			for i in l2:
				hf.write(i+'\r')
			hf.close()
		except Exception, e:
			raise e

	def change_url(self):
		dn = open('diff_new.txt','wb')
		with open('diff.txt','r+') as ft:
			for line in ft.readlines():
				dn.write(line.replace('/home/test/chromiums/old_version/chromium63/third_party/WebKit/LayoutTests','/layout-test-results'))
		ft.close()
		dn.close()

	def change_html(self):
		html = open('index.html', 'w')
		html.write(open('head.html','r').read())
		files = os.listdir('.')

		with open('diff_new.txt','r+') as dn:
			items=[x.split() for x in dn.readlines()][0]
			for line in items:
				html.write("<div>%s<a href='%s'>%s</a>\</div>"%(line[20:30], line, line[5:]))
		dn.close()

		# 然后处理图片
		# for f in files:
		# 	if f.lower().endswith('.jpg') or f.lower().endswith('.png'):
		# 		html.write("<img src='%s'/>" % f)

		html.write('</body></html>')
		html.close()

# print getdata().get_floatfail()